package nickthissen.iracing.adapters;

import java.util.ArrayList;

import nickthissen.iracing.activities.ItemListActivity;
import nickthissen.iracing.datamodels.Driver;
import nickthissen.iracing.iracingforum2.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class DriverListAdapter extends ArrayAdapter<Driver>
{
	private ItemListActivity<Driver> activity;
	private ArrayList<Driver> items;
	
	public DriverListAdapter(ItemListActivity<Driver> context, int resourceId, ArrayList<Driver> items)
	{
		super(context, resourceId, items);
		this.activity = context;
		this.items = items;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View v = convertView;
		if (v == null)
		{
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.driverrow, null);
		}
		
		Driver driver = items.get(position);
		if (driver != null)
		{
			v.setClickable(true);
			v.setOnClickListener(new ItemClickListener(driver));
			v.setOnCreateContextMenuListener(null);
			
			TextView lblTop = (TextView) v.findViewById(R.id.lblTop);				
			lblTop.setMaxLines(1);			
			lblTop.setText(driver.Username);	
		}
		return v;
	}
	
	private class ItemClickListener implements View.OnClickListener
	{
		private Driver driver;
		
		public ItemClickListener(Driver f)
		{
			this.driver = f;
		}
		
		@Override
		public void onClick(View v)
		{
			activity.onItemClicked(driver);
		}		
	}
}
